package yoyu.animationtest;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.airbnb.lottie.LottieAnimationView;

/**
 * 作者: zf617 on 2017/2/7.
 * 邮箱: zhangfeng@putao.com
 */

public class MyWorldView extends RelativeLayout {

    private ImageView appIcon;
    private LottieAnimationView starAnim;
    private ImageView glow;

    private static final long sScaleAnimTime = 400;
    private ObjectAnimator mAppIconScaleXAnim;
    private ObjectAnimator mAppIconScaleYAnim;
    private ObjectAnimator mStarScaleXAnim;
    private ObjectAnimator mStarScaleYAnim;
    private float scaleFirst = 1.10f;
    private float scaleSecond = 1.20f;

    public MyWorldView(Context context) {
        super(context);
    }

    public MyWorldView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWorldView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        appIcon = (ImageView) findViewById(R.id.icon);
        appIcon.setImageResource(R.drawable.my_world);
        starAnim = (LottieAnimationView) findViewById(R.id.star_view);
        starAnim.setAnimation("star_move.json");
        glow = (ImageView) findViewById(R.id.glow);
    }

    public void runAnimation() {
        starAnim.setAnimation("star_move.json");
        starAnim.playAnimation();
        ((AnimationDrawable) glow.getDrawable()).start();
        scaleRepateAnimation();
        iconTransAnimation();
        starTransAnimation();
    }

    private void scaleRepateAnimation() {
        mAppIconScaleXAnim = createScaleAnim(appIcon, "scaleX", scaleFirst, scaleSecond);
        mAppIconScaleYAnim = createScaleAnim(appIcon, "scaleY", scaleFirst, scaleSecond);
        mAppIconScaleXAnim.start();
        mAppIconScaleYAnim.start();

        mStarScaleXAnim = createScaleAnim(starAnim, "scaleX", scaleFirst, scaleSecond);
        mStarScaleYAnim = createScaleAnim(starAnim, "scaleY", scaleFirst, scaleSecond);
        mStarScaleXAnim.start();
        mStarScaleYAnim.start();
    }

    /**
     * create scale anim
     *
     * @param propertyName scaleX or scaleY
     * @param scaleFrom    from scale
     * @param scaleTo      to scale
     * @return ObjectAnimator
     */
    private ObjectAnimator createScaleAnim(View target, String propertyName, float scaleFrom, float scaleTo) {
        final ObjectAnimator mScaleAnim = ofFloat(target, propertyName, scaleFrom, scaleTo);
        mScaleAnim.setDuration(sScaleAnimTime);
        mScaleAnim.setRepeatMode(ValueAnimator.REVERSE);
        mScaleAnim.setRepeatCount(6);

        return mScaleAnim;
    }

    public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
        ObjectAnimator anim = new ObjectAnimator();
        anim.setTarget(target);
        anim.setPropertyName(propertyName);
        anim.setFloatValues(values);
        return anim;
    }

    private void iconTransAnimation() {
        transAnimation(appIcon);
    }

    private void transAnimation(final View view) {
        AnimatorSet anim = new AnimatorSet();
        final float appIconTranY = view.getTranslationY();
        ObjectAnimator transYAnimator = ofFloat(view, "translationY", appIconTranY, appIconTranY - 12);
        transYAnimator.setDuration(200);
        ObjectAnimator transYAnimator1 = ofFloat(view, "translationY", appIconTranY - 12, appIconTranY - 12);
        transYAnimator1.setDuration(1920);

        ObjectAnimator transYAnimator2 = ofFloat(view, "translationY", appIconTranY - 12, appIconTranY);
        transYAnimator2.setDuration(280);

        anim.playSequentially(transYAnimator, transYAnimator1, transYAnimator2);
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {
                view.setTranslationY(appIconTranY);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        anim.start();
    }

    private void starTransAnimation() {
        transAnimation(starAnim);
    }

    public void clickAnimation() {
        AnimatorSet anim = new AnimatorSet();
        ObjectAnimator scaleX = ofFloat(appIcon, "scaleX", 1.0f, 1.45f);
        ObjectAnimator scaleY = ofFloat(appIcon, "scaleY", 1.0f, 1.45f);
        scaleY.setDuration(100);
        scaleX.setDuration(100);
        scaleY.setRepeatMode(ValueAnimator.REVERSE);
        scaleY.setRepeatCount(2);
        scaleX.setRepeatMode(ValueAnimator.REVERSE);
        scaleX.setRepeatCount(2);
        scaleX.start();
        scaleY.start();
        scaleX.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                ObjectAnimator scaleX = ofFloat(appIcon, "scaleX", 1.45f, 1.0f);
                ObjectAnimator scaleY = ofFloat(appIcon, "scaleY", 1.45f, 1.0f);
                scaleY.setDuration(100);
                scaleX.setDuration(100);
                scaleY.start();
                scaleX.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }
}
